package com.ruoyi.topic.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.request.PageRequest;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.topic.domain.entity.TopicPost;
import com.ruoyi.topic.service.ITopicPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.core.web.domain.AjaxResult.success;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xyf
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/topicPost")
public class TopicPostController {

    @Autowired
    private ITopicPostService topicPostService;



    /**
     * 查询议题帖子列表
     */
    @RequiresPermissions("topic:topicPost:list")
    @GetMapping("/list")
    public TableDataInfo list(PageRequest pageRequest) {
        Page<TopicPost> page = topicPostService.page(new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize()));
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }

    /**
     * 导出议题帖子列表
     */
    @RequiresPermissions("topic:topicPost:export")
    @Log(title = "议题帖子", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageRequest pageRequest)
    {
        Page<TopicPost> page = topicPostService.page(new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize()));
        List<TopicPost> list = page.getRecords();
        ExcelUtil<TopicPost> util = new ExcelUtil<TopicPost>(TopicPost.class);
        util.exportExcel(response, list, "议题帖子数据");
    }

    /**
     * 获取议题帖子详细信息
     */
    @RequiresPermissions("topic:topicPost:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        TopicPost TopicPost = topicPostService.getById(id);
        return success(TopicPost);
    }

    /**
     * 新增议题帖子
     */
    @RequiresPermissions("topic:topicPost:add")
    @Log(title = "议题帖子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TopicPost TopicPost) {
        TopicPost.setCreateTime(new Date());
        boolean isSuccess = topicPostService.save(TopicPost);
        return isSuccess ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改议题帖子
     */
    @RequiresPermissions("topic:topicPost:edit")
    @Log(title = "议题帖子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TopicPost TopicPost) {
        boolean isSuccess = topicPostService.updateById(TopicPost);
        return isSuccess ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 删除议题帖子
     */
    @RequiresPermissions("topic:topicPost:remove")
    @Log(title = "议题帖子", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        boolean isSuccess = topicPostService.removeBatchByIds(idList);
        return isSuccess ? AjaxResult.success() : AjaxResult.error();
    }



    
}
